﻿using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using DeploySources.Helper;
using DeploySources.MVVM;
using Microsoft.Win32;

namespace DeploySources.ViewModel
{
	/// <summary>
	/// ViewModel for the ManageConnectionsView
	/// </summary>
	public class ManageConnectionsViewModel : BaseViewModel
	{
		private readonly Window _view;
		private readonly ObservableCollection<ConnectionItem> _connections;

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="view">View for the ViewModel</param>
		public ManageConnectionsViewModel(Window view)
		{
			_view = view;

			var connections = ConnectionSerializer.Deserialize();
			_connections = new ObservableCollection<ConnectionItem>(connections);
		}
		
		/// <summary>
		/// Connections to manage
		/// </summary>
		public ObservableCollection<ConnectionItem> Connections => _connections;

		private int _selectedConnectionIndex;
		/// <summary>
		/// Current selected connection
		/// </summary>
		public int SelectedConnectionIndex
		{
			get { return _selectedConnectionIndex; }
			set
			{
				_selectedConnectionIndex = value;
				OnPropertyChanged("SelectedConnectionIndex");
			}
		}

		private RelayCommand _selectArtefactFileCommand;
		/// <summary>
		/// Select a new Artefact File command
		/// </summary>
		public RelayCommand SelectArtefactFileCommand => _selectArtefactFileCommand ?? (_selectArtefactFileCommand = new RelayCommand(
											   param => SelectArtefactFile()
										   ));
		
		/// <summary>
		/// Select a new Artefact File
		/// </summary>
		private void SelectArtefactFile()
		{
			var currentArtefactFile = Connections[SelectedConnectionIndex].ArtefactFile;
			var openFileDialog = new OpenFileDialog();
			openFileDialog.DefaultExt = ".cs";
			openFileDialog.Filter = "CS Files (*.cs)|*.cs";
			openFileDialog.FileName = currentArtefactFile;

			if (openFileDialog.ShowDialog() == true)
				Connections[SelectedConnectionIndex].ArtefactFile = openFileDialog.FileName;
		}

		private RelayCommand _deleteConnectionCommand;
		/// <summary>
		/// Delete a connection command
		/// </summary>
		public RelayCommand DeleteConnectionCommand => _deleteConnectionCommand ?? (_deleteConnectionCommand = new RelayCommand(
											   param => DeleteConnection()
										   ));
		/// <summary>
		/// Delete a connection
		/// </summary>
		private void DeleteConnection()
		{
			Connections.RemoveAt(_selectedConnectionIndex);
		}

		private RelayCommand _saveCommand;
		/// <summary>
		/// Save changes command
		/// </summary>
		public RelayCommand SaveCommand => _saveCommand ?? (_saveCommand = new RelayCommand(
			                                   param => Save()
		                                   ));

		/// <summary>
		/// Save changes method
		/// </summary>
		private void Save()
		{
			ConnectionSerializer.Serialize(Connections.ToList());
			_view.DialogResult = true;
		}

		private RelayCommand _cancelCommand;
		/// <summary>
		/// Cancel changes command
		/// </summary>
		public RelayCommand CancelCommand => _cancelCommand ?? (_cancelCommand = new RelayCommand(
			                                     param => Cancel()
		                                     ));

		/// <summary>
		/// Cancel changes method
		/// </summary>
		private void Cancel()
		{
			_view.DialogResult = false;
		}
	}
}
